package com.sky.mapper;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.sky.dto.*;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.core.annotation.Order;

import javax.validation.constraints.Pattern;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    @Select("select sum(amount) from orders where status = #{completed} and checkout_time > #{begin} and checkout_time < #{end}")
    Double turnoverStatistic(LocalDateTime begin, LocalDateTime end, Integer completed);

    void submitOrder(Orders orders);

    
    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    
    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    
    /**
     * 分页查询历史订单
     * @param ordersPageQueryDTO
     * @return
     */
    List<OrderVO> historyOrderPageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 根据订单号查询订单明细
     * @param id
     * @return
     */
    @Select("select * from order_detail where order_id = #{id}")
    List<OrderDetail> queryDetailByOrderId(long id);

    /**
     * 根据订单id查询订单详情
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id}")
    Orders queryOrderDetail(long id);

    /**
     * 用户取消订单
     * @param id
     */
    @Update("update orders set status = 6 ,cancel_time = now() ,pay_status = 2 where id = #{id}")
    void cancelOrder(long id);

    /**
     * 统计各个状态的数量
     * @return
     */
    @Select("select * from orders")
    List<Orders> statistic();


    /**
     *商户接单
     * @param ordersConfirmDTO
     */
    @Update("update orders set status = 3 where id = #{id}")
    void confirm(OrdersConfirmDTO ordersConfirmDTO);

    /**
     * 商户拒绝订单
     * @param ordersRejectionDTO
     */
    @Update("update orders set status = 7 , rejection_reason = #{rejectionReason} ,pay_status = 2 where id = #{id}")
    void reject(OrdersRejectionDTO ordersRejectionDTO);

    /**
     * 商户取消订单（未支付）
     * @param ordersCancelDTO
     */
    @Update("update orders set status = 6 , cancel_reason = #{cancelReason} ,cancel_time = now() where id = #{id}")
    void adminCancelOrder1(OrdersCancelDTO ordersCancelDTO);

    /**
     * 商户取消订单（已支付）
     * @param ordersCancelDTO
     */
    @Update("update orders set status = 6 , cancel_reason = #{cancelReason} ,cancel_time = now() , pay_status = 2 where id = #{id}")
    void adminCancelOrder2(Serializable ordersCancelDTO);

    /**
     * 修改为派送
     * @param id
     */
    @Update("update orders set status = 4 where id = #{id}")
    void delivery(long id);

    /**
     * 完成订单
     * @param id
     */
    @Update("update orders set status = 5, delivery_time = now() where id = #{id}")
    void complete(long id);

    /**
     * 查询未支付，下单时间在某个时间之前的订单的订单
     * @param unPaid
     * @param time
     * @return
     */
    @Select("select id from orders where status = #{unPaid} and order_time < #{time}")
    List<Integer> queryUnPaidOrder(Integer unPaid, LocalDateTime time);

    void updateStatus(List<Integer> list);

    /**
     * 查询全部处在派送中(已支付)的订单,支付时间在某个时间之前
     */
    @Select("select * from orders where status = #{status} and checkout_time <#{time}")
    List<Orders> queryAllDeliveryOrder(Integer status,LocalDateTime time);

    /**
     * 修改订单状态为已完成
     * @param list
     */
    void updateDeliveryStatus(List<Orders> list);

    Integer countByMap(Map map);

    @Select("select id from orders where pay_status = #{paid} and checkout_time<#{end} and checkout_time>#{begin}")
    List<Integer> queryOrderByTime(LocalDateTime begin, LocalDateTime end, Integer paid);
}
